/*
 * PlayScenarioPanel.cpp
 *
 *  Created on: Oct 24, 2011
 *      Author: gonzalo
 */

#include "PlayScenarioPanel.h"
#include "SurfaceFactory.h"

PlayScenarioPanel::PlayScenarioPanel(int xPos, int yPos, Widget* parent) :
		ScenarioPanel("Juego Remoto", xPos, yPos, parent) {
	lblNotification = NULL;
}

void PlayScenarioPanel::init(BattleMasterPlayerGame* game) {
	cout << "El escenario es:" << game->getBackgroundPath() << "<-" << endl;
	this->updateBackground(game->getBackgroundPath());

	this->setElements(game->getElements());
}

void PlayScenarioPanel::handleEvent(Event event) {
	//Filtro los que no manejo
	EventType tipoEvento =event.getType();

	//Si no son keyPress los desecho
	if (tipoEvento != KeyPressed || tipoEvento != KeyReleased ) {
		return;
	}


	for (it_widget = widgets.begin(); it_widget != widgets.end(); ++it_widget) {
		((Widget*) *it_widget)->handleEvent(event);
	}
}

void PlayScenarioPanel::resolve() {

	std::list<Bullet*>::iterator aux;
	std::list<Bullet*>::iterator it_bullet;

	//wtf?
	for (it_bullet = bullets.begin(); it_bullet != bullets.end(); ++it_bullet) {
		aux = (it_bullet);
		if (!(*aux)->isDrawable()) {
			//bullets.erase(aux);
		}
	}

	for (it_bullet = bullets.begin(); it_bullet != bullets.end(); ++it_bullet) {
		aux = (it_bullet);
		(*aux)->resolve();
	}
}

void PlayScenarioPanel::showOn(SDL_Surface *surface) {
	SDL_Rect rect;
	rect.x = parcel.posX;
	rect.y = parcel.posY;
	rect.w = parcel.width;
	rect.h = parcel.height;

	SDL_BlitSurface(panel, NULL, surface, &rect);

	for (it_widget = widgets.begin(); it_widget != widgets.end(); ++it_widget) {
		((Widget*) *it_widget)->showOn(surface);
	}

	std::list<Bullet*>::iterator it_bullet;
	for (it_bullet = bullets.begin(); it_bullet != bullets.end(); ++it_bullet) {
		((Widget*) *it_bullet)->showOn(surface);
	}

	if(lblNotification!=NULL){
		lblNotification->showOn(surface);
	}
}

void PlayScenarioPanel::addBullet(int x, int y, int angle) {
	Bullet* bullet = new Bullet(x, y, this, angle,SurfaceFactory::getSurface(SURFACEBULLET));
	bullets.push_back(bullet);
}

void PlayScenarioPanel::setNotification(std::string notification) {
	if (lblNotification != NULL) {
		delete lblNotification;
	}
	lblNotification = new Label(notification, this, 10, 0);
}

PlayScenarioPanel::~PlayScenarioPanel() {
	SDL_FreeSurface(panel);
}
